<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <base href="<%=basePath%>">
    <title>chat.jsp</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<style>
	p.contentdisplay { line-height:200; stext-align:left; color:red; font-size:20pt }
	p.inputzone{text-align:center; color:black; font-size:20pt }
	p.userlist {text-align:center; color:orange; font-size:20pt }
	div{word-wrap:break-word; overflow-y:auto; overflow-x:hidden; border:3 solid #012345; float:left}
	body{margin:0px;}
	</style>
	<script>
	var xmlHttpRequest = null;
	var xmlHttpmsgSend = null;
	var xmlHttpuserlist = null;
	var date = null;
	var array = null;
	var sendtag =true;
	var senduserlisttag = true;
	var msgsendtag=true;
	var i =0;
	var msgsenderrorcount=0;
	var ietag = 0;
	function ajaxRefresh(){
		document.getElementById("chatmsg").focus();
		if(window.ActiveXObject){
			xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			xmlHttpmsgSend = new ActiveXObject("Microsoft.XMLHTTP");
			xmlHttpuserlist = new ActiveXObject("Microsoft.XMLHTTP");
			ietag =1;
		}
		else if(window.XMLHttpRequest){
			xmlHttpRequest = new XMLHttpRequest();
			xmlHttpmsgSend = new XMLHttpRequest();
			xmlHttpuserlist = new XMLHttpRequest();
			ietag =2;
		}
		if(null!=xmlHttpRequest){
			var timeid= setInterval("refreshcondiv()", 1000);
/*			var timeid = setInterval("refreshuserlist()",2000);*/
		}
	}
	function refreshcondiv(){
		xmlHttpRequest.open("GET","chatservlet","true");
		xmlHttpRequest.onreadystatechange= chatcontentResolve;
		if(sendtag){
		sendtag=false;
		xmlHttpRequest.send();
		}
	}
	function chatcontentResolve(){
		if(xmlHttpRequest.readyState == 4){
			if(xmlHttpRequest.status==200){
				var chatcon=document.getElementById("chatcondiv");
				if("null"==xmlHttpRequest.responseText);
					else{
					chatcon.innerHTML = chatcon.innerHTML + xmlHttpRequest.responseText;
					chatcon.scrollTop = chatcon.scrollHeight;
				}
				sendtag=true;
			}
			else{
			alert("状态错误编码"+xmlHttpRequest.status + "请和管理员联系");
			}
		}
	}
	function refreshuserlist(){
		if(i != 0){
			xmlHttpuserlist.open("GET","chatterlist","true");
			xmlHttpuserlist.onreadystatechange= userlistResolve;
			if(senduserlisttag){
				senduserlisttag=false;
				xmlHttpuserlist.send();
			}
		}else{
		 i++;
		}
	}
	
	function userlistResolve(){
		if(xmlHttpuserlist.readyState == 4){
			if(xmlHttpuserlist.status==200){
				var userlistdiv=document.getElementById("userlistdiv");
				if("null"==xmlHttpuserlist.responseText);
				else{
				userlistdiv.innerHTML = xmlHttpuserlist.responseText;
				}
				senduserlisttag=true;
			}
			else{
			alert("状态错误编码"+xmlHttpuserlist.status + "请和管理员联系");
			}
		}
	}
	function chatmsgsubmit(){
		xmlHttpmsgSend.open("GET","chatmsgarray?chatmsg=" + document.getElementById("chatmsg").value +"&ietag=" + ietag,"true");
		xmlHttpmsgSend.onreadystatechange= chatmsgresponseidentify;
		document.getElementById("chatmsg").value="发送中";
		xmlHttpmsgSend.send();
		return false;
	}
	function chatmsgresponseidentify(){
		if(xmlHttpmsgSend.readyState == 4){
			if(xmlHttpmsgSend.status==200){
				if(null!=xmlHttpmsgSend.responseText){
					document.getElementById("chatmsg").value="";
					document.getElementById("chatmsg").focus();
				}else{
					alert("没有相应内容");
				}
			}else{
			alert("发送失败，xmlHttpmsgSend.status" + xmlHttpmsgSend.status +"请和管理员联系");
			}
		}
	}
	function clrscr(){
		document.getElementById("chatcondiv").innerHTML = "";
	}
	</script>
  </head>
  <body onload="ajaxRefresh()">
	<div style="width:79%; height:100%" >
		<div id="chatcondiv" style="width:100%; height:75%; background:#222222" >
		</div>
		<div class="inputzone" style="width:100%; height:25%; background:#333333">
			<br>
			<form onsubmit="return chatmsgsubmit();">
			输入框:<input type="text" id="chatmsg" name="chatmsg" size="60" value="textto发送" />
			<input type="submit" value="确认" />
			</form>
			<button id="clrscr" onclick="clrscr();">&nbsp;&nbsp;清屏&nbsp;&nbsp;</button>

		</div>
	</div>
	<div id="userlistdiv" style="width:19.5%; height:100%; background:brown">
	</div>
  </body>
</html>
